#include "entity.h"
#include "images_diffuse.h"
#include "bullet.h"
#include "hitbox.h"
#include "tag.h"
#include "explosions.h"
#include "enemy.h"

extern entity *player;

declare_tick(target_position, {	
	parent->position = vadd(parent->position, vmul(vsub(self->target, parent->position), 0.08f));// (self->target_y - parent->position.y) * 0.08f;
})

declare_draw(target_position, { })

declare_head(target_position)

target_position *target_loc(vec loc) {
	target_position *tp = c_new(target_position);
	tp->target = loc;
	return tp;
}

declare_body(satellite, {
	size_t period;
})

declare_tick(satellite, {
	if(self->period == 0) {
		make_enemy_bullet(
			get_animator(&sprite_satellite_bullet, 1),
			parent->position,
			vxy(0, -5),
			satellite_explosion
		);
		self->period = 60;
	}
	else --self->period;
})

declare_draw(satellite, { })

declare_head(satellite)

entity *make_satellite_off(vec location, int health, size_t offset) {
	entity *sat = ent_new();
	sat->position = vxy(location.x, 250);
	sat->tag = TAG_ENEMY;
	
	satellite *s = c_new(satellite);
	s->period = (60 + offset) % 60;
	
	add_hitpoint_t(sat, vxy(-6, 6), 3, health);
	add_hitpoint_t(sat, vxy( 6, 6), 2, health);
	add_hitpoint_t(sat, vxy( 0, 6), 1, health);
	
	add_component(sat, get_animator_c(&sprite_satellite, 1, vxy(8, 0)));
	add_component(sat, s);
	add_component(sat, target_loc(location));
	
	add_hitbox(sat, -7, 7, 14, 4);
	
	return sat;
}

entity *make_satellite(vec l, int h) {
	return make_satellite_off(l, h, 0);
}